package lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class loCk降级 {
  static   ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    public static void main(String[] args) throws Exception {
        reentrantReadWriteLock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread() + "【获取写锁】");

            System.out.println(Thread.currentThread() + "写数据到DB或者共享变量");
            TimeUnit.SECONDS.sleep(1);

            reentrantReadWriteLock.readLock().lock();
            System.out.println(Thread.currentThread() + "【获取读锁】");
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
            System.out.println(Thread.currentThread() + "【释放写锁】锁降级结束,降级为读锁");
            // 锁降级结束,降级为读锁
        }

        System.out.println(Thread.currentThread() + "当前线程消费数据，其他读线程（本博客没有实现）此时也可以消费数据");
        TimeUnit.SECONDS.sleep(1);

        System.out.println(Thread.currentThread() + "当前线程消费数据完成，释放读锁");
        reentrantReadWriteLock.readLock().unlock();
    }
}
